<?php
/**
 * Created by PhpStorm.
 * User: zhugeng
 * Date: 2019/6/28
 * Time: 15:27
 */

namespace app\api\controller;

use think\Db;
use think\Request;
use think\facade\Config;
use app\common\service\environmentAnalysis\EnvironmentAnalysisWeatherService;

/**
 * 全要素环境溯源分析-管控和气象影响分析
 * Class EnvironmentAnalysisWeather
 * @package app\api\controller
 */
class EnvironmentAnalysisWeather extends BaseController
{
    /**
     * 污染天数占比
     * zhugeng 2019-06-28
     */
    public function pollutantProportion()
    {
        $param = request()->param();
        if (empty($param['type'])) {
            $this->error('参数错误');
        }
        if ($param['time'] == '') {
            $data = [
                ['name' => '轻度', 'value' => 0],
                ['name' => '中度', 'value' => 0],
                ['name' => '重度', 'value' => 0],
                ['name' => '严重', 'value' => 0]
            ];
        } else {
            $time   = explode(',', $param['time']);
            $num_a1 = 0;
            $num_a2 = 0;
            $num_a3 = 0;
            $num_a4 = 0;
            $num_b1 = [];
            $num_b2 = [];
            $num_b3 = [];
            $num_b4 = [];
            foreach ($time as $k => $v) {
                $aqi    = 'select aqi_level from air_quality_station_aqi_realtime_day where station_id = ' . $param['station_id'] . ' and publish_time = ' . $v;
                $result = Db::query($aqi);
                if (!empty($result)) {
                    if ($result[0]['aqi_level'] == '轻度') {
                        $num_a1   += 1;
                        $num_b1[] = $v;
                    } elseif ($result[0]['aqi_level'] == '中度') {
                        $num_a2   += 1;
                        $num_b2[] = $v;
                    } elseif ($result[0]['aqi_level'] == '重度') {
                        $num_a3   += 1;
                        $num_b3[] = $v;
                    } elseif ($result[0]['aqi_level'] == '严重') {
                        $num_a4   += 1;
                        $num_b4[] = $v;
                    }
                }
            }
            if (empty($num_a1) && empty($num_a2) && empty($num_a3) && empty($num_a4)) {
                $data = [
                    ['name' => '轻度', 'value' => 0],
                    ['name' => '中度', 'value' => 0],
                    ['name' => '重度', 'value' => 0],
                    ['name' => '严重', 'value' => 0]
                ];
            } elseif (!empty($num_a1) && empty($num_a2) && empty($num_a3) && empty($num_a4)) {
                $data = [
                    ['name' => '轻度', 'value' => $num_a1],
                    ['name' => '其他', 'value' => count($time) - $num_a1]
                ];
            } elseif (!empty($num_a1) && !empty($num_a2) && empty($num_a3) && empty($num_a4)) {
                $data = [
                    ['name' => '轻度', 'value' => $num_a1],
                    ['name' => '中度', 'value' => $num_a2],
                    ['name' => '其他', 'value' => count($time) - $num_a1 - $num_a2]
                ];
            } elseif (!empty($num_a1) && !empty($num_a2) && !empty($num_a3) && empty($num_a4)) {
                $data = [
                    ['name' => '轻度', 'value' => $num_a1],
                    ['name' => '中度', 'value' => $num_a2],
                    ['name' => '重度', 'value' => $num_a3],
                    ['name' => '其他', 'value' => count($time) - $num_a1 - $num_a2 - $num_a3]
                ];
            } elseif (!empty($num_a1) && !empty($num_a2) && !empty($num_a3) && !empty($num_a4)) {
                $data = [
                    ['name' => '轻度', 'value' => $num_a1],
                    ['name' => '中度', 'value' => $num_a2],
                    ['name' => '重度', 'value' => $num_a3],
                    ['name' => '严重', 'value' => $num_a4]
                ];
            }
        }
        $this->success('成功', $data);
    }

    /**
     * 获取减排前贡献度
     * @author yy by 2019/7/3
     */
    public function getReduceBeforeContribution()
    {
        $param = $this->request->param();
        # 模拟数据
//        $param = ['start_time' => '2019-06-17', 'end_time' => '2019-06-23', 'region_id' => 53, 'station_id' => 0, 'plan_id' => 1, 'industry_id' => 1, 'is_sulfur'=>1];
        $list = [];
        try {
            $environmentAnalysisWeatherService = new EnvironmentAnalysisWeatherService();
            $list                              = $environmentAnalysisWeatherService->getReduceBeforeContribution($param);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('成功', $list);
    }
}